package com.wk1un2.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wk1un2.entity.Students;
import com.wk1un2.entity.Teachers;
import com.wk1un2.service.LoginService;
import com.wk1un2.util.Result;

@RestController
public class LoginController {
	@Autowired
	private LoginService loginservice;

	@RequestMapping("/login.action")
	public Object login(@RequestParam Map<String, Object> map, HttpSession session) {
		System.out.println(map);
		String user_sf = map.get("user_sf").toString();
		if (user_sf.equals("stu") == true) {
			Students stu = loginservice.loginstu(map);
			if (stu != null) {
				session.setAttribute("STU_SESSION", stu);
				Result res = new Result(0, "", 1, stu);
				return res;
			}
		} else if (user_sf.equals("tea") == true) {
			Teachers tea = loginservice.logintea(map);
			if (tea != null) {
				session.setAttribute("TEA_SESSION", tea);
				Result res = new Result(0, "", 1, tea);
				return res;
			}
		}
		Result res = new Result(1, "", 0, false);
		System.out.println(map);
		return res;
	
	}

	@RequestMapping("/getSession.action")
	public Object getSe(@RequestParam Map<String, Object> map, HttpSession session) {
		Result res = null;
		if (session.getAttribute("STU_SESSION") != null) {
			Students s = (Students) session.getAttribute("STU_SESSION");
			res = new Result(0, "", 1, s);
		} else {
			Teachers s = (Teachers) session.getAttribute("TEA_SESSION");
			res = new Result(0, "", 1, s);
		}
		return res;
	}

	@RequestMapping("/logout.action")
	public Object logout(@RequestParam Map<String, Object> map, HttpSession session) {
		session.invalidate();
		return "1";
	}
}